package com.zhongfl.crm.service.impl;

import com.github.pagehelper.PageHelper;
import com.zhongfl.crm.enums.LxMessageType;
import com.zhongfl.crm.mapper.LxUserMapper;
import com.zhongfl.crm.model.LxUser;
import com.zhongfl.crm.request.LxUserRequest;
import com.zhongfl.crm.service.LxMessageLogService;
import com.zhongfl.crm.service.LxUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.heptagram.core.common.util.JsonUtil;
import tech.heptagram.staff.cas.common.AssertUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 向日葵
 * @Date 2018/9/20
 */
@Service
public class LxUserServiceImpl implements LxUserService {

    @Resource
    private LxUserMapper lxUserMapper;

    @Resource
    private LxMessageLogService lxMessageLogService;

    @Override
    public List<LxUser> findUserList(LxUserRequest lxUserRequest) {
        PageHelper.startPage(lxUserRequest.getPage(),lxUserRequest.getPageSize());
        return lxUserMapper.selectUserList(lxUserRequest);
    }

    @Override
    public LxUser findByLxId(Long lxId) {
        AssertUtils.notNull(lxId,"id为空");
        return lxUserMapper.selectByLxId(lxId);
    }

    @Override
    public List<LxUser> findByDepartmentId(Long departmentId) {
        return lxUserMapper.selectByDepartmentId(departmentId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertUser(LxUser lxUser) {
        lxUserMapper.insertUser(lxUser);

        lxMessageLogService.save(LxMessageType.USER_CREATED, String.valueOf(lxUser.getLxId()), JsonUtil.toJson(lxUser));
    }

    @Override
    public void deletedUserById(Long lxId) {
        lxUserMapper.deletedUserById(lxId);
    }

    @Override
    public void updateUserById(LxUser lxUser) {
        lxUserMapper.updateById(lxUser);
    }

    @Override
    public List<LxUser> findAllUser() {
        return lxUserMapper.selectAllUser();
    }

    /**
     * 将所有匹配状态改为'NO'
     */
    @Override
    public void setAllMatchStatusToNo() {
        lxUserMapper.updateAlltoNo();
    }

    @Override
    public LxUser findByStaffCode(String staffCode) {
        return lxUserMapper.selectByStaffCode(staffCode);
    }
}
